//
//  SEGSplashAdvertController.m
//  SEGUhomeChassis
//
//  Created by 山刘 on 2019/12/5.
//  Copyright © 2019 山刘. All rights reserved.
//

#import "SEGSplashAdvertController.h"
#import "SEGShowadvertManager.h"
#import "GDTSplashAd.h"
#import "GDTSplashAdvertisementView.h"
#import "GDTSplashAdSkipView.h"

@interface SEGSplashAdvertController ()<GDTSplashAdDelegate>

@property (strong, nonatomic) GDTSplashAd *splash;
/// 底部Logo
@property (nonatomic, strong)GDTSplashAdvertisementView *bottomView;
/// "跳过“ 视图
@property (nonatomic, strong) GDTSplashAdSkipView *skipView;
/// 判断方法执行y唯一
@property (nonatomic, assign) BOOL isFinished;

@end

@implementation SEGSplashAdvertController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isFinished = NO;
    
    self.navDisplayType = SEGNavigationBarDisplayTypeHide;
    self.view.backgroundColor = [self colorOfLaunchImage];
    
    [self requesttencentAd];
}

- (void)requesttencentAd {
    if (self.isSoftBoot) {
        [self segInitGDTView];
    }else{
        [[SEGShowadvertManager sharedManager] requestShowadvertWithCode:[@(SEGEnumAdvertismentIdType_Splash) stringValue] finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            if (isSuccessed && codeType && [codeType isEqualToString:@"0"]) {// 展示
                [self segInitGDTView];
            }else{// 不展示
                [self segFinishedAD];
            }
        }];
    }
}

/// 结束广告逻辑
-(void)segFinishedAD{
    NSLog(@"segFinishedAD");
    // BUG #27406 【v4.9.6】iOS 登录成功状态，重新打开APP加载完开屏广告后先跳转下登录界面，再返回至首页
    if (!self.isFinished) {
        if (self.isSoftBoot) {
            [self.navigationController popViewControllerAnimated:NO];
            self.isFinished = YES;
        }else{
            [[SEGUhomeMediator sharedInstance] seg_EnterAppRequestLogic:@{}];
            self.isFinished = YES;
        }
    }
}

-(UIColor*)colorOfLaunchImage{
    NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    NSString *launchImageName = nil;
    for (NSDictionary* dict in imagesDict){
        CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
        if (CGSizeEqualToSize(imageSize, CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)) && [@"Portrait" isEqualToString:dict[@"UILaunchImageOrientation"]]){
            launchImageName = dict[@"UILaunchImageName"];
        }
    }
    return [UIColor colorWithPatternImage:[UIImage imageNamed:launchImageName]];
}

-(void)segInitGDTView {
    self.splash = [[GDTSplashAd alloc] initWithPlacementId:kGDTPosIdLaunch];
    self.splash.delegate = self; //设置代理
    self.splash.fetchDelay = 3; //开发者可以设置开屏拉取时间，超时则放弃展示
    self.splash.backgroundColor = [self colorOfLaunchImage];
    self.bottomView = [[GDTSplashAdvertisementView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 100)];
    self.skipView = [[GDTSplashAdSkipView alloc] initWithFrame:CGRectMake(LYScreenWidth()-65-15, LYScreenHeight()-65, 65, 30)];
    [self.splash loadAd];
}

#pragma mark - GDTSplashAdDelegate
- (void)splashAdSuccessPresentScreen:(GDTSplashAd *)splashAd{
    NSLog(@"开屏广告成功展示,%s",__FUNCTION__);
}
- (void)splashAdDidLoad:(GDTSplashAd *)splashAd{
    NSLog(@"开屏广告素材加载成功,%s",__FUNCTION__);
    BOOL isAdValid = [self.splash isAdValid];
    if (!isAdValid) return;
    UIWindow *fK = [[UIApplication sharedApplication] keyWindow];
    [self.splash showAdInWindow:fK withBottomView:self.bottomView skipView:self.skipView];
}
- (void)splashAdFailToPresent:(GDTSplashAd *)splashAd withError:(NSError *)error{
    NSLog(@"开屏广告展示失败,%s errorCode:%zd,errorMsg:%@",__FUNCTION__,error.code,error.localizedDescription);
    [self segFinishedAD];
}
- (void)splashAdApplicationWillEnterBackground:(GDTSplashAd *)splashAd{
    NSLog(@"应用进入后台时回调，详解: 当点击下载应用时会调用系统程序打开，应用切换到后台,%s",__FUNCTION__);
    [self segFinishedAD];
}
- (void)splashAdExposured:(GDTSplashAd *)splashAd{
    NSLog(@"开屏广告曝光回调,%s",__FUNCTION__);
}
- (void)splashAdClicked:(GDTSplashAd *)splashAd{
    NSLog(@"开屏广告点击回调,%s",__FUNCTION__);
}
- (void)splashAdWillClosed:(GDTSplashAd *)splashAd{
    NSLog(@"开屏广告将要关闭回调,%s",__FUNCTION__);
    [self segFinishedAD];
}
- (void)splashAdClosed:(GDTSplashAd *)splashAd{
    NSLog(@"开屏广告关闭回调,%s",__FUNCTION__);
}
- (void)splashAdWillPresentFullScreenModal:(GDTSplashAd *)splashAd{
    NSLog(@"开屏广告点击以后即将弹出全屏广告页,%s",__FUNCTION__);
}
- (void)splashAdDidPresentFullScreenModal:(GDTSplashAd *)splashAd{
    NSLog(@"开屏广告点击以后弹出全屏广告页,%s",__FUNCTION__);
}
- (void)splashAdWillDismissFullScreenModal:(GDTSplashAd *)splashAd{
    NSLog(@"点击以后全屏广告页将要关闭,%s",__FUNCTION__);
    [self segFinishedAD];
}
- (void)splashAdDidDismissFullScreenModal:(GDTSplashAd *)splashAd{
    NSLog(@"点击以后全屏广告页已经关闭,%s",__FUNCTION__);
}
- (void)splashAdLifeTime:(NSUInteger)time{
    NSLog(@"开屏广告剩余时间回调,%s",__FUNCTION__);
    self.skipView.countDown = time;
    if (time <= 0) {
        [self segFinishedAD];
    }
}
 
@end
